/*created by hp
*on 18-10-13 下午6:30
*/
package com.happts.UI;
import com.happts.Entity.RECORD;
import com.happts.Service.FetchOilResult;
import com.happts.Service.RECORDService;
import com.happts.Utils.CreateExcel;
import jxl.write.WriteException;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Enumeration;

public class FetchOil {


    JTextField nameText, addressText, citeText, numberText, weightText, vegText, refineText, leftVegText, leftRefinedText;
    JLabel nameLabel, addressLabel, citeLabel, numberLabel, weightLabel, leftVegLabel,leftRefinedLabel, vegLabel, refineLabel;
    JPanel p1, p2, pp3;
    ButtonGroup buttonGroup = new ButtonGroup();
    JButton jb_fetch, jb_return;
    JRadioButton bveg, bref;
    private ImageIcon fetch = new ImageIcon("src/com/happts/img/fetch.png");
    ImageIcon ret = new ImageIcon("src/com/happts/img/return.png");

    FetchOil(RECORD record){
        JFrame frame = UtilView.createFrame("取油");
        nameText = new JTextField(12);
        addressText = new JTextField(12);
        citeText = new JTextField(12);
        numberText = new JTextField(12);
        weightText = new JTextField();
        leftVegText = new JTextField();
        leftRefinedText = new JTextField();

        nameLabel = new JLabel("姓名");
        addressLabel = new JLabel("地址");
        citeLabel = new JLabel("代收点");
        numberLabel = new JLabel("单号");
        weightLabel = new JLabel("重量");
        leftVegLabel = new JLabel("剩余菜籽油");
        leftRefinedLabel = new JLabel("剩余精制油");
        vegLabel = new JLabel("菜籽油");
        refineLabel = new JLabel("精制油");
        bveg = new JRadioButton("veg", true);
        bref = new JRadioButton("ref");
        buttonGroup.add(bveg);
        buttonGroup.add(bref);
        jb_fetch = new JButton(fetch);
        jb_return = new JButton(ret);

        //设置布局
        frame.setLayout(null);
        //        frame.setLayout(new GridLayout(3, 2));
        frame.setSize(600, 350);
        frame.setLocation(750, 300);

        int x=30, y=50, w1=30, w2=200, h1= 30;
        nameLabel.setBounds(x, y, w1, h1);
        nameText.setBounds(x+w1+20, y, w2, h1);
        addressLabel.setBounds(x+w1+w2+40, y, w1, h1);
        addressText.setBounds(x+2*w1+w2+60, y, w2, h1);

        citeLabel.setBounds(x, y+h1+20,w1+10, h1);
        citeText.setBounds(x+w1+20, y+h1+20, w2, h1);
        numberLabel.setBounds(x+w1+w2+40, y+h1+20, w1, h1);
        numberText.setBounds(x+2*w1+w2+60,y+h1+20, w2, h1);
        leftVegLabel.setBounds(x-10, y+2*h1+40,w1+30, h1);
        leftVegText.setBounds(x+w1+20, y+2*h1+40, w2, h1);
        leftRefinedLabel.setBounds(x+w1+w2+30, y+2*h1+40,w1+30, h1);
        leftRefinedText.setBounds(x+2*w1+w2+60, y+2*h1+40, w2, h1);

        w2 = 100;
        weightLabel.setBounds(x+180,y+3*h1+60, w1, h1);
        weightText.setBounds(x+w1+200, y+3*h1+60, w2, h1);
        bveg.setBounds(x+w1+10, y+3*h1+60,15, 15);
        vegLabel.setBounds(x+2*w1, y+3*h1+50, 40, h1);

        refineLabel.setBounds(2*x+w1, y+3*h1+80, 40, h1);
        bref.setBounds(x+w1+10, y+3*h1+90 ,15, 15);
        jb_fetch.setBounds(170, 250, 241, 37);
//                jb_login.setBounds(80, 150, 242, 37);
        jb_return.setBounds(500, 280, 75, 30);
//
        nameText.setText(record.getName());nameText.setEnabled(false);
        addressText.setText(record.getAddress());addressText.setEnabled(false);
        citeText.setText(record.getCite());citeText.setEnabled(false);
        numberText.setText(record.getNumber());numberText.setEnabled(false);
        leftVegText.setText(new java.text.DecimalFormat("#.00").format(record.getLeftVeg()));
        leftVegText.setEnabled(false);
        leftRefinedText.setText(new java.text.DecimalFormat("#.00").format(record.getLeftRefined()));
        leftRefinedText.setEnabled(false);

        jb_fetch.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String value="1";
                double weight=0;
                Enumeration<AbstractButton> radioBtns = buttonGroup.getElements();
                boolean typeError=false;
                RECORDService rs = new RECORDService();
                try{

                    weight = Double.valueOf(weightText.getText());

                }catch (NumberFormatException ne){
                    typeError = true;
                    JOptionPane.showMessageDialog(frame, "数值类型错误", "失败", JOptionPane.WARNING_MESSAGE);
                }
                FetchOilResult fr;
                boolean flag=false;
                while (radioBtns.hasMoreElements() && !typeError){
                    AbstractButton btn = radioBtns.nextElement();
                    if (btn.isSelected()){
                        value = btn.getText();
                        if(value.equals("veg")){
                            if(weight < 0 || weight > record.getLeftVeg()) {
                                fr = FetchOilResult.LEFT_NOTENOUGH;
                            }
                            else {
                                fr = rs.fetchOil(record.getCite(), record.getNumber(), true, weight);
                                flag = true;
                            }
                        }else {
                            if (weight < 0 || weight > record.getLeftRefined()) {
                                fr = FetchOilResult.LEFT_NOTENOUGH;
                            }
                            else {
                                fr = rs.fetchOil(record.getCite(), record.getNumber(), false, weight);
                                flag = false;
                            }
                        }

                        if(fr==FetchOilResult.SUCCESS){
                            CreateExcel ce = new CreateExcel();
                            try {
                                ce.fetchOil(record, flag, weight);
                            }
                            catch (WriteException e1) {
                                e1.printStackTrace();
                                JOptionPane.showMessageDialog(frame, "出票失败", "失败", JOptionPane.WARNING_MESSAGE);
                            }
                            catch (IOException e2) {
                                e2.printStackTrace();
                                JOptionPane.showMessageDialog(frame, "出票失败", "失败", JOptionPane.WARNING_MESSAGE);
                            }
                            JOptionPane.showMessageDialog(frame, "取油成功", "成功", JOptionPane.YES_NO_CANCEL_OPTION);
                            frame.dispose();
                            new RecordUI();
                        }
                        else if (fr == FetchOilResult.LEFT_NOTENOUGH)
                            JOptionPane.showMessageDialog(frame, "取油数超过剩余值", "失败", JOptionPane.WARNING_MESSAGE);
                        }

                    }
//                    System.out.println("+"+value);
                }
            }
        );

        jb_return.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                new Menu();
            }
        });

        frame.add(nameLabel);
        frame.add(nameText);//第一块面板添加用户名和文本框

        frame.add(addressLabel);
        frame.add(addressText);//第二块面板添加密码和密码输入框
        frame.add(citeLabel);
        frame.add(citeText);
        frame.add(numberLabel);
        frame.add(numberText);
        frame.add(weightLabel);
        frame.add(weightText);
        frame.add(vegLabel);
        frame.add(leftVegLabel);
        frame.add(leftVegText);
        frame.add(refineLabel);
        frame.add(leftRefinedLabel);
        frame.add(leftRefinedText);

        frame.add(bref);
        frame.add(bveg);
        frame.add(jb_fetch); //第三块面板添加确认和取消
        frame.add(jb_return);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setTitle("取油");
    }
    public static void main(String[] args){
        RECORDService rs = new RECORDService();
        RECORD record = new RECORD("王", "jn","1","6002", 68, 0.8, 0.7);
        new FetchOil(record);
    }
}

